var express = require('express');
var router = express.Router();
var db = require('../sql')
/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/banner',function(req, res, next){
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  db.query('select * from banner order by id desc',function(err,data){
    res.send(data);
  })
})

// 修改轮播图
router.get('/banner/change',function(req, res, next){
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  let img = req.query.img;
  let id = req.query.id;
  console.log('我是img和id',img,id)
  db.query(`update banner set imgSrc=? where id=?`,[img,id],function(err,data){
    res.send(true);
  })
})

// 删除轮播图
router.get('/banner/delect',function(req, res, next){
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  let id = req.query.id; 
  db.query(`delete from banner where id=?`,[id],function(err,data){
    try {
      res.send({
        msg:"delete true",
        code:200
      });
    } catch (err) {
      throw err
    }
  })
})

// 增加轮播图
router.post('/banner/add', function (req, res) {
  res.header("Access-Control-Allow-Origin", req.headers.origin);
  let {imgSrc} = req.body;
  db.query(`insert into banner (imgSrc) 
  values (?)`,
  [imgSrc],
   function (err, data) {
    if(err){
      throw err
    }else{
      res.send(data);
    }
  })
})
module.exports = router;
